﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VotesResults.Daos;
using VotesResults.Daos.EntityFramework;
using VotesResults.Models;

namespace VotesResults.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private readonly IVoteDao _voteDao = new VoteDao();
        private readonly IGroupDao _groupDao = new GroupDao();

        public ActionResult Index()
        {
            return View(
                new HomeIndexModel
                    {
                        Votes = _voteDao.Load(),
                        IsStudent = false,
                        Groups = _groupDao.Load()
                    });
        }

        [HttpPost]
        public ActionResult Index(HomeIndexModel homeIndexModel)
        {
            if (homeIndexModel.SelectedVoteId > 0 && !homeIndexModel.IsStudent)
                return RedirectToAction("Index", "Vote", new { voteId = homeIndexModel.SelectedVoteId, groupId = 0 });
            if (homeIndexModel.SelectedVoteId > 0 && homeIndexModel.IsStudent && homeIndexModel.SelectedGroupId > 0)
                return RedirectToAction("Index", "Vote", new { voteId = homeIndexModel.SelectedVoteId, groupId = homeIndexModel.SelectedGroupId });
            return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
